package com.kangdi.riggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kangdi.riggie.common.Result;
import com.kangdi.riggie.entity.Category;
import com.kangdi.riggie.entity.Dish;
import com.kangdi.riggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public Result<String> save(@RequestBody Category category){
        categoryService.save(category);
        return Result.success("添加成功");
    }
    @GetMapping("page")
    public Result<Page> page(int page,int pageSize){
        Page<Category> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo,categoryLambdaQueryWrapper);
        return Result.success(pageInfo);
    }
    @DeleteMapping
    public Result<String> delete(long ids){
        categoryService.remove(ids);
        return Result.success("删除成功");
    }
    @PutMapping
    public Result<String > update(@RequestBody Category category){
        boolean b = categoryService.updateById(category);
        if (b){
          return  Result.success("修改成功");
        }else{
            return Result.error("修改失败");
        }
    }
    @GetMapping("list")
    public Result<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        List<Category> list = categoryService.list(dishLambdaQueryWrapper);
        return Result.success(list);
    }
}
